﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LibraryMusicStore;
using System.Data.Linq;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraEditors.ViewInfo;

namespace AppMusicStore
{
    public partial class frmReceipt : DevExpress.XtraEditors.XtraForm
    {
        IRepository<Receipt> receiptRepository;

        public frmReceipt()
        {
            InitializeComponent();
        }

        private void frmReceipt_Load(object sender, EventArgs e)
        {
            try
            {
                receiptRepository = new Repository<Receipt>();
                ReceiveData();
            }
            catch (Exception ex)
            {
                Common.ShowErrorBox(ex.Message);
            }
        }

        private void ReceiveData()
        {
            bindingSource1.DataSource = receiptRepository.GetAll()
                .OrderByDescending(r => r.ReceiptId);
                //Common.GetReceipts(db);
        }

        private void Editor_Click(object sender, EventArgs e)
        {
            try
            {
                frmReceiptEditor frm = new frmReceiptEditor();
                frm.ReceiptRepository = this.receiptRepository;

                if (sender.GetType().Name == "SimpleButton")
                    frm.Text = "Thêm phiếu nhập";
                else
                {
                    if (gridView1.GetFocusedRowCellValue("ReceiptId") == null)
                        return;
                    frm.Text = "Sửa phiếu nhập";
                    frm.id = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ReceiptId"));
                }

                if (frm.ShowDialog(this) == DialogResult.OK)
                {
                    receiptRepository = new Repository<Receipt>();
                    ReceiveData();
                }
                frm.Dispose();
            }
            catch (Exception ex)
            {
                Common.ShowErrorBox(ex.Message);
            }
        }

        private void btnDelete_Click()
        {
            try
            {
                DialogResult rs;
                Receipt receipt;

                if (gridView1.SelectedRowsCount > 1)
                {
                    int[] iRows = gridView1.GetSelectedRows();

                    rs = Common.ShowQuestionBox("Xóa các phiếu đã chọn?", "okcancel");

                    if (rs == DialogResult.OK)
                    {
                        foreach (var item in iRows)
                        {
                            receipt = receiptRepository.GetById(
                                Convert.ToInt32(gridView1
                                .GetRowCellValue(item, "ReceiptId")));

                            receiptRepository.DeleteOnSubmit(receipt);
                        }

                        receiptRepository.SubmitChanges();

                        gridView1.DeleteSelectedRows();
                        gridControl1.Update();
                    }
                    return;
                }

                if (gridView1.GetFocusedRowCellValue("ReceiptId") == null)
                    return;

                receipt = receiptRepository.GetById(
                    Convert.ToInt32(gridView1
                    .GetFocusedRowCellValue("ReceiptId")));

                rs = Common.ShowQuestionBox("Xóa phiếu nhập " + receipt
                    .ReceiptId + " ?", "okcancel");

                if (rs == DialogResult.OK)
                {
                    receiptRepository.DeleteOnSubmit(receipt);
                    receiptRepository.SubmitChanges();
                    gridView1.DeleteSelectedRows();
                    gridControl1.Update();
                    MessageBox.Show("Đã xóa", "Thông báo", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                Common.ShowErrorBox(ex.Message);
            }

            //InitGridProducts();
        }

        #region "Context menu item"
        private void InsertToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.Editor_Click(this.btnInsert, new EventArgs());
            btnInsert.PerformClick();
        }

        private void EditToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Editor_Click(this.repositoryItemButtonEdit, new EventArgs());
        }


        private void DeleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnDelete_Click();
        }
        #endregion

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (gridView1.GetFocusedRowCellValue("ReceiptId") == null)
                ChangeVisibleToolTip(false);
            else
                ChangeVisibleToolTip(true);
        }

        private void ChangeVisibleToolTip(bool p)
        {
            EditToolStripMenuItem.Visible = p;
            DeleteToolStripMenuItem.Visible = p;
        }

        private void gridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (Control.ModifierKeys != Keys.None) return;

                var view = sender as GridView;

                if (view.SelectedRowsCount < 2) return;

                GridHitInfo hi = view.CalcHitInfo(new Point(e.X, e.Y));

                if (e.Button == MouseButtons.Left && hi.RowHandle >= 0 &&
                    hi.InRowCell && hi.Column.Name == "colDelete")
                {
                    btnDelete_Click();
                }
            }
            catch (Exception)
            {
                return;
            }
        }

        private void repositoryItemButtonDelete_Click(object sender, EventArgs e)
        {
            btnDelete_Click();
        }

    }
}